﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace BBICMS.Model.Validation
{
    public class IdentityCardAttribute : RegularExpressionAttribute, IClientValidatable
    {
        public IdentityCardAttribute()
            : base(@"d{15}|d{18}")
        {
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var errorMessage = FormatErrorMessage(metadata.GetDisplayName());

            yield return new IdentityCardValidationRule(errorMessage);
        }
    }
    public class IdentityCardValidationRule : ModelClientValidationRule
    {
        public IdentityCardValidationRule(string errorMessage)
        {
            ErrorMessage = errorMessage;
            ValidationType = "identitycard";
        }
    }
}